/** QC ??
 * 06-Apr-2011 Created
 * 
 */
package com.munoor.dalai.server.api;

import javax.persistence.EntityManager;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import com.munoor.dalai.server.model.Company;
import com.munoor.dalai.server.model.EMFService;
import com.munoor.dalai.server.model.Listing;
import com.munoor.dalai.server.model.ListingMetadata;
import com.munoor.dalai.server.model.ListingView;
import com.munoor.dalai.server.model.wrappers.WrappedEntity;
import com.munoor.dalai.server.model.wrappers.WrappedListing;
import com.munoor.dalai.server.model.wrappers.WrappedListingMetadata;
import com.munoor.dalai.server.model.wrappers.WrappedListingView;
import com.sun.jersey.api.NotFoundException;

/**
 * @author Osho Ilamah
 * @since version 1.0 (April 2011)
 */
public class ListingResource {
	@Context
	UriInfo uriInfo;
	@Context
	Request request;
	Long id;
	
	public ListingResource(UriInfo uriInfo, Request request, Long id) {
		this.uriInfo = uriInfo;
		this.request = request;
		this.id = id;
	}	
	
	@GET
	@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
	public WrappedEntity readListing(@QueryParam("type") @DefaultValue("0") int type) {
		EntityManager em = EMFService.get().createEntityManager();
		try {
			Listing listing = ((Listing) em.createNamedQuery("Listing.findByListingId").setParameter("listingId", id).getSingleResult());
			WrappedEntity wrappedListing = null;
			switch (type){
		    case 0:
		    	ListingMetadata metadata = new ListingMetadata();
				metadata.setTitle(listing.getTitle());
				metadata.setCompany(((Company) em.createNamedQuery("Companies.findByCompanyId").setParameter("companyId", listing.getCompanyId()).getSingleResult()).getCompanyName());
				metadata.setListingId(listing.getListingId());
				wrappedListing = new WrappedListingMetadata(metadata);
		    	break;
		    case 1:
				ListingView listingView = new ListingView(listing);
				listingView.setCompany(((Company) em.createNamedQuery("Companies.findByCompanyId").setParameter("companyId", listing.getCompanyId()).getSingleResult()).getCompanyName());
				wrappedListing = new WrappedListingView(listingView);
		    	break;
		    case 2:
		    	wrappedListing = new WrappedListing(listing);
		    }			
			return wrappedListing;
		} catch (Exception e) {
			throw new NotFoundException("Not Found");
		} finally {
			em.close();
		}
	}
	
	//ADMIN CALLS ONLY
	
	@PUT
	@Consumes(MediaType.APPLICATION_JSON)
	public Response updateListing(WrappedListing postData) {
		if (uriInfo.getPath().indexOf("/admin/listings/") == -1) {
			throw new NotFoundException("Not Found");
		}
		EntityManager em = EMFService.get().createEntityManager();
		try {
			//Listing listing = ((Listing) em.createNamedQuery("Listings.findByListingId").setParameter("listingId", id).getSingleResult());
			Listing listing = postData.unmarshall();
			listing.setListingId(id);
			em.merge(listing);
			return Response.noContent().build();
		} catch (Exception e) {
			return Response.serverError().build();
		}finally{
			em.close();
		}
	}
	
	@DELETE
	public Response deleteListing() {
		if (uriInfo.getPath().indexOf("/admin/listings/") == -1) {
			throw new NotFoundException("Not Found");
		}		
		EntityManager em = EMFService.get().createEntityManager();
		try {
			Listing listing = (Listing) em.createNamedQuery("Listing.findByListingId").setParameter("listingId", id).getSingleResult();
			em.remove(listing);
			return Response.noContent().build();
		} catch (Exception e) {
			throw new NotFoundException("Not Found");
		}finally{
			em.close();
		}
	}
	
}
